package com.itheima.security;

import com.itheima.pojo.User;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MyUserService implements UserDetailsService {
    private Map<String, User> initData(){

        Map<String, com.itheima.pojo.User> map = new HashMap<>();

        com.itheima.pojo.User user = new com.itheima.pojo.User();
        user.setUsername("lisi");
        user.setPassword("{noop}1234");

        map.put(user.getUsername(),user);

        com.itheima.pojo.User user1 = new com.itheima.pojo.User();
        user1.setUsername("zs");
        user1.setPassword("{noop}1234");
        map.put(user1.getUsername(),user1);
        return  map;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        com.itheima.pojo.User user = initData().get(username);
        if(null == user){
            //没有查询到用户返回null，框架自己处理
            return null;
        }


        List<SimpleGrantedAuthority> authorities = new ArrayList<>();

        if(username.equals("lisi")){
            authorities.add(new SimpleGrantedAuthority("新增检查项"));
        } else if(username.equals("zs")){
            authorities.add(new SimpleGrantedAuthority("查询检查项"));
        }


        return new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),authorities);
    }

}
